package org.viva.service.cms;

import java.io.File;

import org.springframework.stereotype.Service;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class FileEditService {

    public JSONArray toDir(String root, String dirPath) {
        String filedirpath = root + dirPath;
        filedirpath = filedirpath + File.separator;
        File dir = new File(filedirpath);
        File[] list = dir.listFiles();
        JSONArray array = new JSONArray();
        if (list != null) {
            for (File file : list) {
                JSONObject jo = new JSONObject();
                jo.put("text", file.getName());
                if (file.isDirectory()) {
                    jo.put("icon", "fa-folder");
                    jo.put("type", "dir");
                    jo.put("children", true);
                } else {
                    jo.put("icon", "fa-file");
                    jo.put("type", "file");
                    jo.put("children", false);
                }
                jo.put("id", file.getAbsolutePath().replace(root, ""));
                // jo.put("size", file.length() / (1024) + "kb");
                // jo.put("lastModified", UTIL.FORMAT.date(file.lastModified(),
                // "yyyy/MM/dd"));
                array.add(jo);
            }
        }
        return array;
    }

}
